home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / icon tools / wbinfo / wbinfo.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  5KB  |  175 lines

  1. /*
  2.  * $VER: WBInfo.c 42.3 (13.9.95)
  3.  * by Bruce M. Simpson
  4.  *
  5.  * doesn't bother checking for root / volume case
  6.  *
  7.  * NOT FINISHED - AND YES, THERE ARE BUGS IN HERE
  8.  *
  9.  * The bugs are usually caused by the CurrentDir() code. They don't seem to
  10.  * do any harm though. No memory leakage, but haven't tested under Mungwall.
  11.  *
  12.  * Doesn't bother calling Examine(), and just uses PathPart()/AddPart(), so
  13.  * you may have to type in the full path of an object. I'm working on this
  14.  * though.
  15.  *
  16.  * Won't back out correctly if you try to obtain information on a
  17.  * non-filesystem device, no memory lost, but funny lock behaviour.
  18.  * Doesn't seem to harm the system though - but 'TurboList' generates
  19.  * spurious bad block errors when run. I 'CD' back and it's fine - it seems
  20.  * to have CD'd to SYS: ...
  21.  *
  22.  *
  23.  * todo:
  24.  *    multiple arguments using pattern matching
  25.  *    handle 'disk.info' case for root directory
  26.  *
  27.  */
  28.  
  29. #define __USE_SYSBASE
  30. #include <exec/types.h>
  31. #include <exec/alerts.h>
  32. #include <dos/dos.h>
  33. #include <proto/exec.h>
  34. #include <proto/dos.h>
  35. #include <proto/intuition.h>
  36. #include <proto/wb.h>
  37.  
  38. #define NAMEBUFSIZE 256
  39. static const TEXT verstag[] = "\0$VER: WBInfo 42.3 (13.9.95)";
  40. static const TEXT ProgName[] = "WBInfo";
  41. static const TEXT cmdtemplate[] = "OBJECT/A";
  42.  
  43.  
  44. ULONG   main(void)
  45. {
  46.     struct ExecBase *SysBase = (*((struct ExecBase **) 4L));
  47.     struct Process *MyTask;
  48.     struct DosLibrary *DOSBase;
  49.     struct IntuitionBase *IntuitionBase;
  50.     struct Library *WorkbenchBase;
  51.     struct Screen *screen;
  52.     struct RDArgs *rdargs;
  53.     BPTR    objlock;
  54.     BPTR    parentlock;
  55.     BPTR    olddir;
  56.     TEXT    name[NAMEBUFSIZE];    /* 256 char buffer */
  57.     ULONG   rc = RETURN_FAIL;
  58.     STRPTR  arg_name;
  59.  
  60.     MyTask = (struct Process *) FindTask(NULL);
  61.  
  62.     if (DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L))
  63.     {
  64.         if (IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37L))
  65.         {
  66.             if (WorkbenchBase = OpenLibrary("workbench.library", 39L))
  67.             {
  68.                 if (rdargs = ReadArgs(cmdtemplate, (LONG *) &arg_name, NULL))
  69.                 {
  70.                     if (*((TEXT *)arg_name))
  71.                     {
  72.                         /* insert pattern matching code here */
  73.                         /* insert public screen option code here */
  74.  
  75.                         if (screen = LockPubScreen(NULL))
  76.                         {
  77.                             register ULONG size;
  78.  
  79.                             if (size = PathPart(arg_name) - arg_name)
  80.                             {
  81.                                 CopyMem(arg_name, &name, size);
  82.                                 name[((UWORD) size)] = '\0';
  83.  
  84.                                 parentlock = Lock(name, ACCESS_READ);
  85.                                 olddir = CurrentDir(parentlock);
  86.                             }
  87.                             else
  88.                             {
  89.                                 /* object is in current directory - fake a CD
  90.                                  * to simplify cleanup operations
  91.                                  */
  92.                                 parentlock = DupLock(MyTask->pr_CurrentDir);
  93.                                 olddir = CurrentDir(MyTask->pr_CurrentDir);
  94.                             }
  95.  
  96.                             /* sanity check on parentlock here, please */
  97.                             if (parentlock)
  98.                             {
  99.                                 if (objlock = Lock(FilePart(arg_name), ACCESS_READ))
  100.                                 {
  101.                                     /* insert device / volume check here */
  102.                                     if (NameFromLock(objlock, name, NAMEBUFSIZE))
  103.                                     {
  104.                                         WBInfo(parentlock, FilePart(name), screen);
  105.                                         rc = RETURN_OK;
  106.                                     }
  107.                                     else
  108.                                         /* NameFromLock failed */
  109.                                         PrintFault(IoErr(), ProgName);
  110.  
  111.                                     UnLock(objlock);
  112.                                 }
  113.                                 else
  114.                                     /* Lock on the object failed */
  115.                                     PrintFault(IoErr(), ProgName);
  116.  
  117.                                 /* CD back to old current dir and free parent object lock */
  118.                                 CurrentDir(olddir);
  119.                                 UnLock(parentlock);
  120.                             }
  121.                             else
  122.                                 /* Lock on parent directory of object failed */
  123.                                 PrintFault(IoErr(), ProgName);
  124.  
  125.                             UnlockPubScreen(NULL, screen);
  126.                         }
  127.                         else
  128.                         {
  129.                             /* no screen found */
  130.                             SetIoErr(ERROR_OBJECT_NOT_FOUND);
  131.                             PrintFault(ERROR_OBJECT_NOT_FOUND, ProgName);
  132.                         }
  133.                     }
  134.                     else
  135.                     {
  136.                         /* we were given a null string as input argument */
  137.                         SetIoErr(ERROR_REQUIRED_ARG_MISSING);
  138.                         PrintFault(ERROR_REQUIRED_ARG_MISSING, ProgName);
  139.                         rc = RETURN_ERROR;
  140.                     }
  141.                     FreeArgs(rdargs);
  142.                 }
  143.                 else
  144.                 {
  145.                     /* ReadArgs failed */
  146.                     PrintFault(IoErr(), ProgName);
  147.                     rc = RETURN_ERROR;
  148.                 }
  149.                 CloseLibrary(WorkbenchBase);
  150.             }
  151.             else
  152.             {
  153.                 /* no workbench library */
  154.                 Alert(AG_OpenLib | AO_Workbench);
  155.                 SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY);
  156.                 PrintFault(ERROR_INVALID_RESIDENT_LIBRARY, ProgName);
  157.             }
  158.             CloseLibrary((struct Library *) IntuitionBase);
  159.         }
  160.         else
  161.         {
  162.             /* no intuition library */
  163.             Alert(AG_OpenLib | AO_Intuition);
  164.             SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY);
  165.             PrintFault(ERROR_INVALID_RESIDENT_LIBRARY, ProgName);
  166.         }
  167.         CloseLibrary((struct Library *) DOSBase);
  168.     }
  169.     else
  170.         /* no dos library */
  171.         Alert(AG_OpenLib | AO_DOSLib);
  172.  
  173.     return (rc);
  174. }
  175.